<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader :buttons="['quickSearch', 'columnDisplay']" quick-search-placeholder="搜索 商品名称">
            <template #btnBefore>
            </template>
            
            <!-- <span v-if="state.show_goods == 'all'" style="margin-left: 12px;" class="btn btn-default" @click="showGoods('collection')">收藏记录</span> -->
            <!-- <span v-if="state.show_goods == 'collection'" style="margin-left: 12px;" class="btn btn-info" @click="showGoods('all')">收藏记录</span> -->
            <!-- <span style="margin-left: 12px;" class="btn" @click="showImportLogFun()">导入记录</span> -->
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">
            <template #size-stock>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column prop="id" label="库存数量" width="" align="center">
                    <template #default="scope">
                        <div style="text-align: center;">
                            <table border="1" style="text-align: center; border-color: #fff; font-weight: normal; margin: 0 auto;" cellspacing="0" >
                                <tr>
                                    <td v-for="(item, key) in scope.row.specs" :key="key" style="width: 54px;" width="100" v-html="item.size"></td>
                                </tr>
                                <tr>
                                    <td v-for="(item, key) in scope.row.specs" :key="key" style="" v-html="item.stock"></td>
                                </tr>
                            </table>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #cycle>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column prop="id" label="生产周期" width="180" align="center">
                    <template #default="scope">
                        <div style="">
                            <table border="1" style="width: 100%; text-align: center; border-color: #fff;" cellspacing="0" >
                                <tr>
                                    <td style="width: 50%;">15天</td>
                                    <td style="width: 50%;">30天</td>
                                </tr>
                                <tr>
                                    <td>{{ scope.row.cycle_15 }}</td>
                                    <td>{{ scope.row.cycle_30 }}</td>
                                </tr>
                            </table>
                        </div>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />

        <ImportFrom v-if="!state.loading" v-model="state.showImportFrom" />
        <!-- <ImportLog v-if="!state.loading" v-model="state.showImportLog" /> -->
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

import { collectionApi, cancelCollectionApi } from '/@/api/frontend/goods/goods'

import ImportFrom from './import.vue'
import ImportLog from './import_log.vue'

defineOptions({
    name: 'goods/goods',
})

const { t } = useI18n()
const tableRef = ref()


const state = reactive({
    showImportFrom: false,
    showImportLog: false,
    loading: false,
    show_goods: 'all',
});

let optButtons = defaultOptButtons([])

// 自定义一个新的按钮
let newButton: OptButton[] = [
    // {
    //     render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
    //     name: 'download', // 按钮名称
    //     title: 'download', // 鼠标放置时的 title 提示
    //     class: 'btn margin-left-none',
    //     text: '收藏', // 直接在按钮内显示的文字，title 有值时可为空
    //     type: '', // 按钮类型，请参考 element plus 的按钮类型
    //     icon: '', // 按钮 icon
    //     disabledTip: true, // tipButton 禁用 tip
    //     click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
    //         console.log('collection')
    //         // collectionFun(row.id)
    //         collectionApi(row.id).then((e) => {
    //             console.log(e)
    //             if(e.code == 1){
    //                 row.collection = 1;
    //             }
    //         })
    //     },
    //     display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
    //         return row.collection == null ? true : false;
    //     },
    //     disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
    //         // return row.file ? false : true;
    //     },
    //     attr: {} // 自定义el-button属性
    // },{
    //     render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
    //     name: 'download', // 按钮名称
    //     title: 'download', // 鼠标放置时的 title 提示
    //     class: 'btn margin-left-none',
    //     text: '取消收藏', // 直接在按钮内显示的文字，title 有值时可为空
    //     type: '', // 按钮类型，请参考 element plus 的按钮类型
    //     icon: '', // 按钮 icon
    //     disabledTip: true, // tipButton 禁用 tip
    //     click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
    //         console.log('cancel collection')
    //         cancelCollectionApi(row.id).then((e) => {
    //             console.log(e)
    //             if(e.code == 1){
    //                 row.collection = null;
    //             }
    //         })
    //     },
    //     display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
    //         return row.collection == null ? false : true;
    //     },
    //     disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
    //         // return row.file ? false : true;
    //     },
    //     attr: {} // 自定义el-button属性
    // },
    // {
    //     render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
    //     name: 'edit',
    //     title: 'edit',
    //     text: '编辑',
    //     type: 'primary',
    //     class: 'btn btn-primary margin-left-none',
    //     icon: '',
    // },{
    //     render: 'confirmButton',
    //     name: 'delete',
    //     title: '',
    //     text: '删除',
    //     type: 'danger',
    //     class: 'btn btn-danger margin-left-none',
    //     icon: '',
    //     popconfirm: {
    //         confirmButtonText: t('Delete'),
    //         cancelButtonText: t('Cancel'),
    //         confirmButtonType: 'danger',
    //         title: t('Are you sure to delete the selected record?'),
    //     }
    // }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)

// const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])




const showGoods = (type) => {
    state.show_goods = type;
    baTable.table.filter!.show_goods = type;
    baTable.getIndex();
}


const showImportFromFun = () => {
    state.showImportFrom = true;
    return false;
}

const showImportLogFun = () => {
    state.showImportLog = true;
    return false;
}

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/user/goods.Goods/'),
    {
        pk: 'id',
        column: [
            // { type: 'selection', align: 'center', operator: false },
            { label: t('品牌名'), prop: 'brand.name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('季度编码'), prop: 'quarter_code', width: 90, align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { 
                label: t('图片'), 
                render: "image", 
                prop: 'cover', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE', 
                sortable: false,
                width: 120
            },
            { label: t('商品名'), prop: 'name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { 
                label: t('颜色'), 
                prop: 'id', 
                align: 'center', 
                operator: false, 
                render: "string" , 
                renderFormatter: (row: TableRow, field: TableColumn, value: any, column: TableColumnCtx<TableRow>, index: number) => {
                    return row.specs[0].color;
                    let color = '';
                    for(let i = 0; i < row.specs.length; i++){
                        if(color == ''){
                            color = row.specs[i].color;
                        }else{
                            color += ', ' + row.specs[i].color;
                        }
                    }
                    return color;
                }
            },
            { 
                label: t('尺码'), 
                prop: 'id', 
                align: 'center', 
                operator: false, 
                render: "string" , 
                renderFormatter: (row: TableRow, field: TableColumn, value: any, column: TableColumnCtx<TableRow>, index: number) => {
                    let size = '';
                    for(let i = 0; i < row.specs.length; i++){
                        if(size == ''){
                            size = row.specs[i].size;
                        }else{
                            size += ', ' + row.specs[i].size;
                        }
                    }
                    return size;
                }
            },
            { label: t('官方售价'), prop: 'price', align: 'center', operator: 'RANGE', sortable: false },
            
            { label: t('拿货价格'), prop: 'user_price', align: 'center', operator: 'RANGE', sortable: false },
            { 
                label: t('库存数量'), 
                prop: 'supply_discount', 
                align: 'center', 
                operator: 'RANGE', sortable: false, 
                render: 'slot', 
                slotName: 'size-stock'
            },
            
            { 
                label: t('生产周期'), 
                prop: 'cycle_15', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE', 
                sortable: false,
                render: 'slot', 
                slotName: 'cycle'
            },
            // { 
            //     label: t('goods.goods.create_time'), 
            //     prop: 'create_time', 
            //     align: 'center', 
            //     render: 'datetime', 
            //     operator: 'RANGE', 
            //     sortable: false, 
            //     width: 100, 
            //     timeFormat: 'mm-dd/h:MM'
            // },
            // { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: ['all'],
        defaultOrder: { prop: 'id', order: 'desc' },
    },
    {
        defaultItems: { name: null, color: null, size: null, stock: null, stock_total: null, cover: null, price: 0, supply_discount: 0, supply_price: 0, cycle_15: null, cycle_30: null, upstream_link: null },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">
:deep(.el-image){
    width: 85px;
    height: unset;
}
:deep(.ba-data-table .margin-left-none){
    margin-left: 0!important;
    margin-bottom: 8px;
    padding: 4px 14px;
}
:deep(.ba-data-table .ml-6){
    margin-left: 0!important;
}


:deep(.ba-data-table .margin-left-none:last-child){
    margin-bottom: 0px;
}
:deep(.table-operate-text){
    padding-left: 0;
}
:deep(.font-size-13){
    font-size: 13px;
}


.ba-table-box{
    margin-bottom: 0px;
}

:deep(.el-table__inner-wrapper){
    height: calc(100vh - 197px);
}

@media screen and (min-width: 768px){
    :deep(.el-table__inner-wrapper){
        height: calc(100vh - 220px);
    }
}

</style>
